package com.stox.ui.javafx.navigator;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import jfxtras.labs.scene.control.window.CloseIcon;
import jfxtras.labs.scene.control.window.Window;

public class JavafxTestEnv extends Application{

	public static void main(String[] args) {
		launch(args);
	}
	
	private Window window;
	private Node node;
	
	@Override
	public void start(Stage stage) throws Exception {
		stage.setMaximized(true);
		
		BorderPane borderPane = new BorderPane();
		Scene scene = new Scene(borderPane);
		stage.setScene(scene);
		
		MenuBar menubar = new MenuBar();
		Menu fileMenu = new Menu("File");
		menubar.getMenus().add(fileMenu);
		MenuItem exitMenuItem = new MenuItem("Exit");
		exitMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				System.exit(0);
			}
		});
		
		fileMenu.getItems().add(exitMenuItem);
		
		Menu viewMenu = new Menu("View");
		menubar.getMenus().add(viewMenu);
		MenuItem navigatorMenuItem = new MenuItem("Navigator");
		navigatorMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				navigatorAction();
			}
		});
		viewMenu.getItems().add(navigatorMenuItem);
		borderPane.setTop(menubar);
		
		Pane pane = new StackPane();
		borderPane.setCenter(pane);
		
		HBox messagePane = new HBox();
		messagePane.setBackground(new Background(new BackgroundFill[] {new BackgroundFill(Color.DARKGRAY, new CornerRadii(10), new Insets(5))}));
		messagePane.setPrefHeight(30);
		borderPane.setBottom(messagePane);
		
		window = new Window("Navigator");
		window.setPrefSize(400, 400);
		
		window.setMinWidth(400);
		window.setMinHeight(400);
		
		window.getRightIcons().add(new CloseIcon(window));
		window.getContentPane().getChildren().add(new Button("test"));
		

		window.boundsInParentProperty().addListener(new ChangeListener<Bounds>(){
			private boolean locked;
			@Override
			public void changed(ObservableValue<? extends Bounds> arg0,
					Bounds oldValue, Bounds newValue) {
				if(locked || oldValue.equals(newValue)) return;
				locked = true;
				try {
					Bounds paneBounds = pane.getBoundsInParent();
					Bounds windowBounds = window.getBoundsInParent();
					
					if(windowBounds.getMinX() < paneBounds.getMinX())
						window.setLayoutX(paneBounds.getMinX());
					if(windowBounds.getMaxX() > paneBounds.getMaxX())
						window.setLayoutX(paneBounds.getMaxX() - windowBounds.getWidth());
					if(windowBounds.getMinY() < paneBounds.getMinY())
						window.setLayoutY(paneBounds.getMinY());
					if(windowBounds.getMaxY() > paneBounds.getMaxY())
						window.setLayoutY(paneBounds.getMaxY() - windowBounds.getHeight());
				}finally {
					locked = false;
				}
			}
		});
		borderPane.getChildren().add(window);
		
		stage.show();
		node = window;
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				navigatorAction();
				window.getParent().requestLayout();
				navigatorAction();
			}
		});
	}

	
	
	private void navigatorAction() {
		System.out.println(node.getBoundsInLocal());
		System.out.println(node.getBoundsInParent());
		System.out.println(node.getLayoutBounds());
		System.out.println();
	}
	
	
	
	
}
